const weekdateDict = { 0: "日", 1: "一", 2: "二", 3: "三", 4: "四", 5: "五", 6: "六" };

// 格式化时间，第一个参数是想格式化的时间，第二个参数是想要格式化的格式
function formatTime(time, format) {
  // 判断传入转为时间是否Invalid Date，是的话直接返回
  time = new Date(time);
  if (time == "Invalid Date") {
    return time;
  }
  const year = time.getFullYear();
  const month = time.getMonth() + 1;
  const day = time.getDate();
  const hour = time.getHours();
  const minute = time.getMinutes();
  const second = time.getSeconds();

  const formatMap = {
    yyyy: year.toString(),
    MM: month.toString().padStart(2, "0"),
    dd: day.toString().padStart(2, "0"),
    HH: hour.toString().padStart(2, "0"),
    mm: minute.toString().padStart(2, "0"),
    ss: second.toString().padStart(2, "0"),
    yy: year.toString().slice(-2),
    M: month.toString(),
    d: day.toString(),
    H: hour.toString(),
    m: minute.toString(),
    s: second.toString(),
  };

  const dict = ['yyyy', 'MM', 'dd', 'HH', 'mm', 'ss', 'yy', 'M', 'd', 'H', 'm', 's'];

  for (const key of dict) {
    format = format.replace(new RegExp(key, "g"), formatMap[key]);
  }

  return format;
}

// 获取指定日期的明天
function getTomorrow(date, format) {
  const tomorrow = new Date(date);
  format = format || 'yyyy-MM-dd';
  tomorrow.setDate(tomorrow.getDate() + 1);
  return formatTime(tomorrow, format);
}
// 获取指定日期的昨天
function getYesterday(date, format) {
  const yesterday = new Date(date);
  format = format || 'yyyy-MM-dd';
  yesterday.setDate(yesterday.getDate() - 1);
  return formatTime(yesterday, format);
}


export {
  weekdateDict,
  formatTime,
  getTomorrow,
  getYesterday,
};
